package com.kinghood.authoritycenter.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kinghood.authoritycenter.common.model.dao.sys.SysDirectionCategoryDO;
import com.kinghood.authoritycenter.common.model.dto.sys.SystemDirectionCategoryReqDTO;
import com.kinghood.authoritycenter.common.model.vo.sys.SysDirectionCategoryVO;
import com.kinghood.authoritycenter.dao.sys.SysDirectionCategoryMapper;
import com.kinghood.authoritycenter.service.ISysDirectionCategoryService;
import com.kinghood.infrastructure.model.KingHoodException;
import com.kinghood.infrastructure.model.constant.CommonConstants;
import com.kinghood.infrastructure.model.constant.ExceptionConstant;
import com.kinghood.infrastructure.util.base.IdGenUtil;
import com.kinghood.infrastructure.util.UserUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import com.kinghood.authoritycenter.common.model.dao.sys.SysDirectionDO;

/**
 * <p>
 * 系统数据字典表 服务实现类
 * </p>
 *
 * @author KingHood
 * @since 2023-12-20
 */
@Service
public class SysDirectionCategoryServiceImpl extends ServiceImpl<SysDirectionCategoryMapper, SysDirectionCategoryDO> implements ISysDirectionCategoryService {
    @Resource
    private SysDirectionServiceImpl sysDirectionService;

    /**
     * 查询所有规格列表
     *
     * @author jp
     * @date 2023/12/21
     */
    @Override
    public List<SysDirectionCategoryVO> queryAllDirectionCategory() {
        List<SysDirectionCategoryDO> nativeList = this.lambdaQuery().eq(SysDirectionCategoryDO::getIsDelete, CommonConstants.IS_DELETE_N)
                .list();
        List<SysDirectionCategoryVO> result = nativeList.stream().map(i -> {
            SysDirectionCategoryVO item = new SysDirectionCategoryVO();
            BeanUtils.copyProperties(i, item);
            return item;
        }).collect(Collectors.toList());
        return result;
    }

    /**
     * 根据ID查询详情
     *
     * @param id id
     * @author jp
     * @date 2023/12/21
     */
    @Override
    public SysDirectionCategoryVO detailDirection(Long id) {
        SysDirectionCategoryDO category = this.getById(id);
        if (Objects.nonNull(category)) {
            SysDirectionCategoryVO vo = new SysDirectionCategoryVO();
            BeanUtils.copyProperties(category, vo);
            return vo;
        }
        return null;
    }

    /**
     * 根据id删除规格
     *
     * @param id id
     * @author jp
     * @date 2023/12/21
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delDirectionByid(Long id) {
        LocalDateTime now = LocalDateTime.now();
        SysDirectionCategoryDO category = this.getById(id);
        if (Objects.nonNull(category)) {
            if (category.getStatus() == CommonConstants.ENABLE) {
                throw new KingHoodException(-1 , "只有停用状态的才可操作删除");
            }
            //删除字典类别
            this.lambdaUpdate().set(SysDirectionCategoryDO::getIsDelete, CommonConstants.IS_DELETE_Y)
                    .set(SysDirectionCategoryDO::getUpdateTime, now)
                    .set(SysDirectionCategoryDO::getUpdateUserId, UserUtil.getUserId())
                    .eq(SysDirectionCategoryDO::getId, id)
                    .update();
            sysDirectionService.lambdaUpdate().set(SysDirectionDO::getIsDelete, CommonConstants.IS_DELETE_Y)
                    .set(SysDirectionDO::getUpdateTime, now)
                    .set(SysDirectionDO::getUpdateUserId, UserUtil.getUserId())
                    .eq(SysDirectionDO::getDataCode, category.getDataCode())
                    .update();
        }
    }

    /**
     * 新增更新规格
     *
     * @param dirReq dir红色
     * @author jp
     * @date 2023/12/21
     */
    @Override
    public void addorUpdate(SystemDirectionCategoryReqDTO dirReq) {
        SysDirectionCategoryDO sd = new SysDirectionCategoryDO();
        if(Objects.isNull(dirReq.getId())) {
            long count = this.lambdaQuery().eq(SysDirectionCategoryDO::getDataCode, dirReq.getDataCode())
                    .eq(SysDirectionCategoryDO::getIsDelete, CommonConstants.IS_DELETE_N)
                    .count();
            if(count > 0 ) {
                throw new KingHoodException(ExceptionConstant.CODE_BAD_SERVER , "已经有此编码，请勿重复新增");
            }
            BeanUtils.copyProperties(dirReq, sd);
            sd.setId(IdGenUtil.get().nextId());
            sd.setCreateTime(LocalDateTime.now());
            this.save(sd);
        }else {
            BeanUtils.copyProperties(dirReq, sd);
            sd.setCreateTime(LocalDateTime.now());
            this.updateById(sd);
        }
    }
}
